//
//  OosCheckinEntryParser.m
//  oos4ios
//
//  Created by Pablo Gonzalez on 4/19/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import "OosCheckinEntryParser.h"
#import <oos4ios/Model/OosService.h>
#import <oos4ios/Model/OosCheckin.h>
#import <oos4ios/Model/OosUser.h>
#import <oos4ios/Model/OosAvatarImage.h>
#import "../Atom/Entries/OosCheckinEntry.h"
#import "OosServiceEntryParser.h"

@implementation OosCheckinEntryParser

static OosCheckinEntryParser *instance;

+ (OosCheckinEntryParser *)instance
{
    if (!instance) {
        instance = [[[OosCheckinEntryParser alloc] init] retain];
    }
    return instance;
}

- (id)parseEntry:(OosEntry *)entry
{
    OosCheckinEntry *checkinEntry = (OosCheckinEntry *) entry;
	OosUser *user = nil;
	OosService *service = nil;
    OosAvatarImage *avatar = nil;
    OosCheckin *checkin = nil;
    
    NSDate *updated = [checkinEntry.updatedDate date];
    checkinEntry.updatedDate = nil;
    if (checkinEntry.author.slug == nil) {
        user = nil;
    }
    user = [OosUser userFromSlug:checkinEntry.author.slug];
    user.nick = checkinEntry.author.name;
    avatar = [OosAvatarImage avatarFromPath: checkinEntry.oosAuthor.avatarUrl];
    user.avatar = avatar;
    service = [[OosServiceEntryParser instance] parseEntry: checkinEntry];
    checkin = [OosCheckin checkinFromUser:user andService:service];
    
    checkin.updated = updated;
    checkin.count = [checkinEntry.count intNumberValue];
    return checkin;
}

- (OosEntry *)reverseParseEntry:(id)object
{
    if (object) {
        OosEntry *entry = [OosEntry entry];
        // TODO see if it is needed to create the entry (probably not)
        return entry; 
    }
    return nil;
}

#pragma mark - json addons

- (id)parseJsonEntry:(NSDictionary *)jsonEntry
{
    OosCheckin *checkin = nil;
    
    NSDateFormatter* formatter = [[self class] atomFormatter];
    
    NSDate *updated = [formatter dateFromString:[jsonEntry objectForKey:@"updated"]];
    NSString *slug = [jsonEntry objectForPath:@"author/name"];
    
    
    
    return checkin;
}

@end
